//
//  InformationTopView.m
//  Vlink
//
//  Created by 赵隆杰 on 2021/9/22.
//  Copyright © 2020 myTV. All rights reserved.
//

#import "InformationTopView.h"
#import "SDCycleScrollView.h"
#import "InformationDetailViewController.h"

@interface InformationTopView()<SDCycleScrollViewDelegate>

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;

@end

@implementation InformationTopView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        if (!_cycleScrollView) {
            _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(20, 10, SL_WINDOW_WIDTH - 40, 8.0/15*(SL_WINDOW_WIDTH - 40)) delegate:self placeholderImage:placeholder_image];
//            _cycleScrollView.backgroundColor = [UIColor blueColor];
            _cycleScrollView.autoScrollTimeInterval = 3;
            _cycleScrollView.pageDotColor = [UIColor grayColor];
            _cycleScrollView.currentPageDotColor = [UIColor whiteColor];
            _cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
            _cycleScrollView.placeholderImage = placeholder_image;
            _cycleScrollView.pageDotColor = [UIColor colorWithRed:255/256.0 green:255/256.0 blue:255/256.0 alpha:.5];
            _cycleScrollView.currentPageDotColor = [UIColor colorWithRed:255/256.0 green:255/256.0 blue:255/256.0 alpha:1.0];

            //backgroundImageView contentmode 不对
            Ivar ivar = class_getInstanceVariable([SDCycleScrollView class], "_backgroundImageView");
            UIImageView *backgroundImageView = object_getIvar(_cycleScrollView, ivar);
            backgroundImageView.contentMode = UIViewContentModeScaleToFill;
            _cycleScrollView.layer.cornerRadius = 6;
            _cycleScrollView.clipsToBounds = YES;
            [self addSubview:_cycleScrollView];
            
            //        _cycleView = [[Carousel alloc]initWithFrame:CGRectMake(0, 0, SL_WINDOW_WIDTH, _viewModel.bannerHeight)];
            //        _cycleView.needPageControl = YES;
            //        _cycleView.infiniteLoop = YES;
            //        _cycleView.pageControlPositionType = PAGE_CONTROL_POSITION_TYPE_MIDDLE;
            //        _cycleView.delegate = self;
            //        _cycleView.placeHolderImageName = @"Liveback";
        }
    }
    return self;
}

- (void)setViewModel:(InformationViewModel *)viewModel
{
    _viewModel = viewModel;
        
    NSMutableArray *picList = [NSMutableArray array];
    for (InformationModel *model in viewModel.modelList) {
        NSString *imgUrl = model.messagePic?:@"";
        [picList addObject:imgUrl];
    }
    self.cycleScrollView.imageURLStringsGroup = picList;

}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    if (_viewModel.modelList.count > index) {
        
        InformationModel *model = _viewModel.modelList[index];
        InformationDetailViewController *vc = [[InformationDetailViewController alloc] init];
        vc.model = model;
        [[UIWindow currentViewController].navigationController pushViewController:vc animated:YES];
    }
}


+(CGFloat)height
{
    CGFloat imgH = 8.0/15*(SL_WINDOW_WIDTH - 40);
    return 10 + imgH + 10;
    // 上面间距16，下面间距10
}


@end
